package org.andes.kownledge.operator.executable;

import java.util.ArrayList;
import java.util.List;

import org.andes.base.Bindings;
import org.andes.kownledge.operator.Executable;
import org.andes.kownledge.operator.NoAtom;
import org.andes.kownledge.operator.Term;
import org.andes.kownledge.operator.func.Func;
import org.andes.sgg.st;

public class Test extends Executable{
	private List<NoAtom> funcs=new ArrayList<NoAtom>();
	public Test(){
		
	}
	public Test(NoAtom func1){
		funcs.add(func1);
	}
//	(defun execute-test (E State)
//			   "Implements the (test <function>) condition of operators."
//			   (if (eval (subst-bindings-quoted (st-bindings State) (second E)))
//			       (list State)))
	@Override
	public List<st> exec(st state) {
		NoAtom func=funcs.get(0);
		
		
		Term subsFunc=func.substBindings(state.getBindings());
		if(subsFunc instanceof Func){
			Object result=((Func)subsFunc).doFunc();
			if(result!=null){
				List<st> states=new ArrayList<st>();
				states.add(state);
				return states;
			}else			
				return null;
		}else if(subsFunc instanceof Executable) {
			return ((Executable)subsFunc).exec(state);
		}else
			return null;
		
	}
	
	public void addFunc(NoAtom func2) {
		funcs.add(func2);
		
	}
	@Override
	public Term substBindings(Bindings bindings) {
		Test result=new Test();
		for(NoAtom fun:funcs){
			result.addFunc((NoAtom) fun.substBindings(bindings));
		}
		return result;
	}
}
